package com.example.treehole.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.treehole.bean.Chat;
import com.example.treehole.bean.Message;
import com.example.treehole.common.BaseResult;
import com.example.treehole.service.ChatServiceImpl;
import com.example.treehole.service.MessageServiceImpl;
import com.example.treehole.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Consumer;

@RestController
@RequestMapping("/chat")
public class ChatController {


    @Autowired
    private ChatServiceImpl chatService;
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private MessageServiceImpl messageService;


    @GetMapping("getMessage")
    public ResponseEntity<?> getMessage(String chatId, String userid) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getChatId, chatId);
        messageService.lambdaUpdate()
                .eq(Message::getChatId, chatId)
                .eq(Message::getToId, userid)
                .set(Message::getUnread, 0)
                .update();

        List<Message> list = messageService.list(queryWrapper);
        for (Message message : list) {
            message.setUser(userService.getById(message.userid));
        }
        return BaseResult.ok("获取成功", list);
    }

    @PostMapping("sendMessage")
    public ResponseEntity<?> sendMessage(@RequestBody Message message) {
        message.unread = 1;
        messageService.save(message);
        return BaseResult.ok("发送成功");
    }
//获取一个具体的聊天框
    @GetMapping("getById")
    public ResponseEntity<?> getById(String userid1, String userid2) {
        LambdaQueryWrapper<Chat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(chatLambdaQueryWrapper -> chatLambdaQueryWrapper.eq(Chat::getUserid1, userid1)
                        .eq(Chat::getUserid2, userid2))
                .or(chatLambdaQueryWrapper -> chatLambdaQueryWrapper.eq(Chat::getUserid1, userid2)
                        .eq(Chat::getUserid2, userid1));
        Chat one = chatService.getOne(queryWrapper);
        if (one == null) {
            Chat entity = new Chat();
            entity.setUserid1(userid1);
            entity.setUserid2(userid2);
            chatService.save(entity);
            Chat one1 = chatService.getOne(new LambdaQueryWrapper<Chat>().eq(Chat::getUserid1, userid1).eq(Chat::getUserid2, userid2));
            one1.setMine(userService.getById(userid1));
            one1.setOther(userService.getById(userid2));
            return BaseResult.ok("", one1);
        } else {
            one.setMine(userService.getById(userid1));
            one.setOther(userService.getById(userid2));
            return BaseResult.ok("", one);
        }
    }

    @GetMapping("get")
    public ResponseEntity<?> get(String userid) {
        LambdaQueryWrapper<Chat> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chat::getUserid1, userid).or().eq(Chat::getUserid2, userid);
        List<Chat> list = chatService.list(queryWrapper);
        for (Chat chat : list) {
            chat.setUnread(messageService.lambdaQuery()
                    .eq(Message::getToId, userid)
                    .eq(Message::getUnread, 1)
                    .eq(Message::getChatId, chat.id).count());
            if (userid.equals(chat.getUserid1())) {
                chat.setMine(userService.getById(chat.getUserid1()));
                chat.setOther(userService.getById(chat.getUserid2()));
            } else {
                chat.setMine(userService.getById(chat.getUserid2()));
                chat.setOther(userService.getById(chat.getUserid1()));
            }
        }
        return BaseResult.ok("获取成功", list);
    }

}
